package pers.csw.lookBook.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.csw.lookBook.mapper.BookRackMapper;
import pers.csw.lookBook.mapper.BooksMapper;
import pers.csw.lookBook.mapper.ChapterMapper;
import pers.csw.lookBook.dto.BookRackDto;
import pers.csw.lookBook.entity.BookRack;
import pers.csw.lookBook.entity.Books;
import pers.csw.lookBook.entity.Chapter;
import pers.csw.lookBook.service.BookRackService;

import java.util.ArrayList;
import java.util.List;

@Service
public class BookRackServiceImpl extends ServiceImpl<BookRackMapper, BookRack> implements BookRackService {
    @Autowired
    BookRackMapper bookRackMapper;
    @Autowired
    BooksMapper booksMapper;
    @Autowired
    ChapterMapper chapterMapper;

    @Override
    public List<BookRackDto> getBookRackByUserId(Integer userId) {
        List<BookRackDto> bookRackDtos = new ArrayList<>();
        bookRackMapper.getBookRackByUserId(userId).forEach(bookRack -> {
            BookRackDto bookRackDto = new BookRackDto();
            Books books = booksMapper.selectById(bookRack.getBookId());
            bookRackDto.setBookRack(bookRack);
            bookRackDto.setBooks(books);
            Chapter chapter = chapterMapper.selectById(bookRack.getChapterId());
            bookRackDto.setChapter(chapter);
            bookRackDtos.add(bookRackDto);
        });
        return bookRackDtos;
    }

    @Override
    public BookRack getBookRackByUserIdAndBookId(Integer userId, Integer bookId) {
        return bookRackMapper.getBookRackByUserIdAndBookId(userId,bookId);
    }
}
